// pages/xqlist/xqlist.js
const app = getApp();
const url = app.globalData.url;
const common = require("../../utils/common.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loding:true,
    zhs:false,
    zhwz:'综合',
    jzwz:'数据已经加载完毕...',
    ck:1,
    px:'ASC',
    islb:1,
    sq:'',
    fid:'',
    flid:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (o) {
    console.log(o)
    let that=this;
    wx.getStorage({
      key: 'xqislb',
      success(res) {
        console.log(res);
        that.setData({ islb: res.data })
      }
    })
    this.peizi();
    if(o.fid){
      this.setData({fid:o.fid})
    }
    if(o.flid){
      this.setData({flid:o.flid})
    }
    if(o.lx){
      if(o.lx=='zd'){
        this.zdbtn();
      }
      if(o.lx=='zx'){
        this.zxbtn();
      }
    }else{
      this.hqlist();
    }
    // this.setData({loding:false})
  },
  zhbtn(){
    if(this.data.zhs){
      this.setData({zhs:false})
    }else{
      if(this.data.ck==1){
        this.setData({zhs:true})
      }else{
        this.mrbtn();
      }
    }
  },
  lbbtn(){
    if (this.data.islb==1){
      this.setData({ islb: 2})
      wx.setStorage({
        key: 'xqislb',
        data: 2,
      });
    }else{
      this.setData({ islb: 1})
      wx.setStorage({
        key: 'xqislb',
        data: 1,
      });
    }
  },
  zxbtn(){
    if(this.data.ck!=2){
      this.setData({ zhs: false, ck: 2,})
      this.hqlist();
    }
  },
  jgbtn(){
    if(this.data.ck!=3){
      this.setData({ zhs: false, ck: 3,})
      this.hqlist();
    }else if(this.data.px =='ASC'){
      this.setData({ px:"DESC" })
      this.hqlist();
    } else if (this.data.px == 'DESC') {
      this.setData({ px: "ASC" })
      this.hqlist();
    }
  },
  // 跳转url
  tourl(e) {
    let url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url
    })
  },
  mrbtn(){
    this.setData({ zhs: false, ck: 1, zhwz: '综合', sq:''});
    this.hqlist();
  },
  zdbtn(){
    this.setData({ zhs: false, ck: 1, zhwz: '置顶', sq:' AND iszd=1 '});
    this.hqlist();
  },
  bqbtn(e){
    // console.log(e.currentTarget.dataset.bqid)
    // console.log(e.currentTarget.dataset.tt)
    var tt = e.currentTarget.dataset.tt;
    var bqid = e.currentTarget.dataset.bqid;
    var sq=" AND bqid= "+bqid;
    this.setData({ zhs: false, ck: 1, zhwz: tt, sq:sq });
    this.hqlist();
  },
  //获取列表
  hqlist(sx) {
    let that = this;
    let list = []
    let pno = 1;
    let sq = that.data.sq;
    let ck=that.data.ck;
    let px=that.data.px;
    if (sx) {
      list = that.data.list;
      pno = that.data.pno;
    }

    let sj = {
      pno: pno,
      ck: ck,
      px:px,
      fid:that.data.fid,
      flid:that.data.flid,
      sq: sq,
      pgs: 10
    };
    app.fetch('api/wx/getxqlist', sj, "POST", (err, data) => {
      console.log(data)
      for (var i = 0; i < data.data.length; i++) {
        if (data.data[i].ewm) {
          data.data[i].ewm = url + data.data[i].ewm;
        }
        data.data[i].addtime = common.bijiao(data.data[i].addtime);
        if (data.data[i].logolist) {
          var arr = data.data[i].logolist.split(",");
          data.data[i].logolist = [];
          let ls = arr.length < 5 ? arr.length : 5;
          for (let a = 0; a < ls; a++) {
            data.data[i].logolist.push(url + arr[a]);
          }
        } else {
          data.data[i].logolist = [];
          // data[i].imgs.push(data[i].tx);
        };
        list.push(data.data[i]);
      }
      that.setData({
        list: list,
        pno: Number(data.pno) + 1,
        pcount: Number(data.pageCount),
        loding: false
      });
      wx.hideLoading();
    })
  },
   // 跳转搜索
   tosearch(){
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  // 配置文件
  peizi: function () {
    let that = this;
    app.fetch('api/wx/getpeizi', { dlx: 'xcxindex' }, "GET", (err, data) => {
      console.log(data)
      if (data.code == 200) {
        wx.setNavigationBarTitle({
          title: data.data.xcx.title.txt
        });
        that.setData({peizi:data.data});
        that.hqbqfl();
      } else {
        wx.showModal({
          title: '接口数据错误',
          content: '接口:"' + url + 'api/wx/getpeizi.php' + '",msg:' + data.msg,
          showCancel: false,//是否显示取消按钮
          confirmText: "好的",//默认是“确定”
          confirmColor: 'skyblue',//确定文字的颜色
          success: function (res) {

          },
          fail: function (res) { },//接口调用失败的回调函数
          complete: function (res) { },//接口调用结束的回调函数（调用成功、失败都会执行）
        })
      }
    })
  },
  hqbqfl() {
    let that = this;
    let sj = {
      fldj: 3,
      lx: 1,
      pgs: 50,
    };
    app.fetch('api/wx/getfl', sj, "POST", (err, data) => {
      console.log(data)
      var list = [];
      for (var i = 0; i < data.data.length; i++) {
        list[i] = data.data[i];
      }
      that.setData({
        'flbq': list,
      });
      this.setData({ loding: false });
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var count = that.data.pcount;
    var pno = that.data.pno;
    console.log(pno, count)
    if (pno - 1 < count) {
      wx.showLoading({
        title: "正在加载中....",
        // mask: true
      });
      that.hqlist('sx');
    } else {
      that.setData({ jzs: true });
      setTimeout(function () {
        that.setData({ jzs: false });
      }, 1000)
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})